/*
 * main.c
DDenns plugin by darvin
 */
#include "main.h"




PLUGIN_API int XPluginStart(
						char *		outName,
						char *		outSig,
						char *		outDesc)
{


	strcpy(outName, "DDenn Aircraft Plugin");
	strcpy(outSig, "__FILE__ compiled at __DATE__ __TIME__");
	strcpy(outDesc, getFileNameinPluginDir("datarefs_save.dat"));

			//"A plugin for realise of DDen's aircrafts, by nbdarvin@gmail.com");

	/* Open a file to write to.  We locate the X-System directory
	 * and then concatenate our file name.  This makes us save in
	 * the X-System directory.  Open the file. */


#ifdef DDENN_DUMMY_DATAREFS
	DdennDummyDataRefs::RegisterDdennDummyDataRefs();
#endif

#ifdef DDENN_LOCAL_TIME
	DdennLocalTime::RegisterDdennLocalTime();
#endif

#ifdef DDENN_INS_GARMIN_GMA_340
	DdennGarminGma340::RegisterDdennGarminGma340();
#endif

#ifdef DDENN_INS_GARMIN_GTX_327
	DdennGarminGtx327::RegisterDdennGarminGtx327();
#endif

#ifdef DDENN_INS_ACCELEROMETER_AM9
	DdennAm9::RegisterDdennAm9();
#endif

#ifdef DDENN_COMMANDS
	DdennCommands::DdennCommandsDisable();
#endif

#ifdef DDENN_TIMERS
	DdennTimers::RegisterDdennTimers();
#endif

#ifdef DDENN_TIMERS_DOWN
	DdennTimersDown::RegisterDdennTimersDown();
#endif

#ifdef DDENN_DATAREFS_SAVE
	DdennDatarefsSave::RegisterDatarefsSave();
#endif

	/* Register our callback for once a second.  Positive intervals
	 * are in seconds, negative are the negative of sim frames.  Zero
	 * registers but does not schedule a callback for time. */
	XPLMRegisterFlightLoopCallback(
			MyFlightLoopCallback,	/* Callback */
			60.0,					/* Interval */
			NULL);					/* refcon not used. */

	return 1;
}

PLUGIN_API void	XPluginStop(void)
{
	/* Unregister the callback */
	XPLMUnregisterFlightLoopCallback(MyFlightLoopCallback, NULL);

#ifdef DDENN_DATAREFS_SAVE
	DdennDatarefsSave::UnregisterDatarefsSave();
#endif


#ifdef DDENN_DUMMY_DATAREFS
	DdennDummyDataRefs::UnregisterDdennDummyDataRefs();
#endif

#ifdef DDENN_LOCAL_TIME
	DdennLocalTime::UnregisterDdennLocalTime();
#endif

#ifdef DDENN_INS_GARMIN_GMA_340
	DdennGarminGma340::UnregisterDdennGarminGma340();
#endif

#ifdef DDENN_INS_GARMIN_GTX_327
	DdennGarminGtx327::UnregisterDdennGarminGtx327();
#endif

#ifdef DDENN_INS_ACCELEROMETER_AM9
	DdennAm9::UnregisterDdennAm9();
#endif

#ifdef DDENN_COMMANDS
	DdennCommands::DdennCommandsEnable();
#endif

#ifdef DDENN_TIMERS
	DdennTimers::UnregisterDdennTimers();
#endif

#ifdef DDENN_TIMERS_DOWN
	DdennTimersDown::UnregisterDdennTimersDown();
#endif



}






PLUGIN_API void XPluginDisable(void)
{
	/* Flush the file when we are disabled.  This is convenient; you
	 * can disable the plugin and then look at the output on disk. */
//	DdennCommands::DdennCommandsEnable();
}

PLUGIN_API int XPluginEnable(void)
{
//	DdennCommands::DdennCommandsDisable();
	return 1;
}

PLUGIN_API void XPluginReceiveMessage(
					XPLMPluginID	inFromWho,
					long			inMessage,
					void *			inParam)
{

	if ((inMessage>=XPLM_MSG_AIRPORT_LOADED)||(inMessage==XPLM_MSG_PLANE_LOADED)||(inMessage==XPLM_MSG_PLANE_CRASHED)){
#ifdef DDENN_DATAREFS_SAVE
	DdennDatarefsSave::onLoad();
#endif
	}
	if (inMessage>=XPLM_MSG_PLANE_UNLOADED){
#ifdef DDENN_DATAREFS_SAVE
	DdennDatarefsSave::UnregisterDatarefsSave();
#endif
	}
}

float	MyFlightLoopCallback(
                                   float                inElapsedSinceLastCall,
                                   float                inElapsedTimeSinceLastFlightLoop,
                                   int                  inCounter,
                                   void *               inRefcon)
{



	/* Return 1.0 to indicate that we want to be called again in 1 second. */
	return 0;
}
